<?php
/**
 * 文章列表控制器
 * @author Mortimer He <mortimerhe@163.com>
 */
namespace Manage\Controller;
use Common\Library\Category;
use Common\Library\Page;
class ArticleController extends CommonController{
	private $Model;
	private $msg=array();
	function _initialize(){
		parent::_initialize();
		$this->Model=D('Article');
	}
	public function main(){
		$this->redirect('index');
	}
	/**
	 * 列表
	 */
	public function index(){
		if(!$this->checkPermission('Article')) $this->jump();
		$data=array();
		if(I('get.title')) $data['title']=I('get.title');
		if(I('get.status')) $data['status']=I('get.status');
		if(I('get.article_category_id')) $data['article_category_id']=I('get.article_category_id');
		$filter_data=I('get.');
		if(isset($filter_data['article_category_id'])){
			$article_category=D('ArticleCategory')->getArticleCategory($filter_data['article_category_id']);
		}
		$url=array(
			'title'	=>	I('get.title','','trim'),
			'status'=>	I('get.status','','trim'),
			'article_category_id'	=>	I('get.article_category_id','','trim')
			);
		$filter_data['article_category_title']=$article_category['article_category_description'][get_default_lng_id()]['title'];
		$count=$this->Model->getCount($data);
		$Page=new Page($count,C('CONFIG_LIMIT_MANAGE'));
		$Page->setConfig('header',L('page_header'));
		$Page->setConfig('theme',L('page_theme'));
		$data['start']=$Page->firstRow;
		$data['limit']=$Page->listRows;
		$list=$this->Model->getArticles($data);
		foreach ($list as $k => $v) {
			$list[$k]['thumb']=image_resize($v['image'],100,100);
		}
		$show=$Page->show();
		$this->assign('filter_data',$filter_data);
		$this->assign('list',$list);
		$this->assign('url',$url);
		$this->assign('page',$show);
		$this->display();

	}
	/**
	 * 修改和新增
	 */
	public function oper($article_id=0){
		if(!$article_id && !$this->checkPermission('AddArticle')) $this->jump();			
		$languages=get_langs();
		$article=$this->Model->getArticle($article_id);
		//如果有article_id参数，却查询不出内容，则article_id肯定是胡乱输入的
		if($article_id && empty($article)) $this->jump(L('error_404'));
		if(IS_POST){
			$article=I('post.');
			if($this->vilidateForm()){
				$this->Model->operArticle($article,$article_id);
				session('success',$article_id?L('success_edit'):L('success_add'));
				$url=array(
					'title'	=>	I('get.title','','trim'),
					'status'=>	I('get.status','','trim'),
					'article_category_id'	=>	I('get.article_category_id','','trim')
					);
				$this->redirect('index',$url);
			}else{
				//如果验证时返回了warning的值说明权限验证没有通过
				if(!isset($this->msg['warning'])) $this->msg['warning']=L('error_warning');
			}
		}
		$article['thumb']=image_resize($article['image'],100,100);
		$article['placeholder']=image_resize();
		foreach ($article['article_image'] as $k => $v) {
			$article['article_image'][$k]['thumb']=image_resize($v['image'],100,100);
			$article['article_image'][$k]['text']=json_decode($v['text'],true);
		}
		$language_id=get_default_lng_id();
		foreach ($article['article_related'] as $k => $v) {
			$article_info=$this->Model->getArticle($v);
			$article['article_related'][$k]=array(
				'article_id'	=>	$v,
				'title'			=>	$article_info['article_description'][$language_id]['title']
				);
		}
		$where=array('article_id'=>(int)$article_id);
//		$article_download=M('ArticleToDownload')->where($where)->select();
//		$article['article_download']=array();
//		if($article_download){
//			$DownloadModel=D('Download');
//			foreach($article_download as $download){
//				$download_info=$DownloadModel->getDownload($download['download_id']);
//				$article['article_download'][]=array(
//					'download_id'	=>	$download['download_id'],
//					'name'			=>	$download_info['download_description'][$language_id]['name']
//					);
//			}
//		}
		$this->assign('languages',$languages);	
		$article_categories=D('ArticleCategory')->getArticleCategories();
		$article_categories=Category::unlimitedForLevel($article_categories,'',0,0,'article_category_id','parent_id');
		$this->assign('article_categories',$article_categories);
		$this->assign('msg',$this->msg);
		$this->assign('article',$article);
		$this->display();
	}

	/**
	 * 数据验证
	 */
	private function vilidateForm(){
		$article_id=I('get.article_id',0,'trim');
		if($article_id && !$this->checkPermission('EditArticle')){
			$this->msg['warning']=L('error_not_permission');
			return $this->msg;
		}
		$data=I('post.');
		foreach($data['article_description'] as $language_id => $value){
			if(mb_strlen($value['title'])<2 || mb_strlen($value['title'])>255){
				$this->msg['title'][$language_id]=L('error_title');
			}
			if(mb_strlen($value['meta_title'])>255){
				$this->msg['meta_title'][$language_id]=L('error_meta_title');
			}
			if(mb_strlen(mb_strlen($value['meta_description'])>255)){
				$this->msg['meta_description'][$language_id]=L('error_meta_description');
			}
			if(mb_strlen($value['meta_keyword'])>255){
				$this->msg['meta_keyword'][$language_id]=L('error_meta_keyword');
			}

		}
		return !$this->msg;
	}

	/**
	 * 删除
	 */
	public function del(){
		if($this->checkPermission('DeleteArticle')){
			$count=0;
			if($this->vilidateDelete()){
				foreach (I('post.selected') as $article_id) {
					$count += $this->Model->del($article_id);
				}
				if($count){
					session('success',L('success_delete',array('count'=>$count)));
				}
			}else{
				session('danger',L('error_delete'));
			}
		}else{
			session('danger',L('error_not_permission'));
		}
		$this->redirect('index');
	}

	/**
	 * 删除验证
	 */
	private function vilidateDelete(){
		return true;
	}

	/**
	 * 自动完成
	 */
	public function autocomplete(){
		$json=array();
		$filter_data=array(
			'start'			=>	0,
			'limit'			=>	8,			
			);
		if(I('get.filter_title')){
			$filter_data['title']=I('get.filter_title');
		}
		if(I('get.article_category_id')){
			$filter_data['article_category_id']=I('get.article_category_id');
		}
		if(I('get.status')){
			$filter_data['status']=I('get.status');
		}

		$articles=$this->Model->getArticles($filter_data);

		foreach ($articles as $v) {
			$json[]=array(
				'article_id'=>	$v['article_id'],
				'article_category_id'=>	$v['article_category_id'],
				'title'		=>	$v['title']
				);
		}
		$this->ajaxReturn($json,'JSON');
	}
}